package horizon.graphics;

import horizon.entity.Camera;
import horizon.util.Vector2;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import javax.imageio.ImageIO;

public class RenderingContext {
    
    Camera cam = new Camera("Camera",new Vector2(0 , 0) , 1f);
    private Graphics2D g = null;

    public Graphics2D getGraphics() {
        return g;
    }
    private HashMap<String, BufferedImage> textures = new HashMap<String, BufferedImage>();

    public RenderingContext(Graphics2D g) {

        this.g = g;
        loadTextures();
    }

    private void loadTextures() {


        String files;
        File folder = new File("res");
        File[] listOfFiles = folder.listFiles();
        
        
        
        if(listOfFiles != null){
            for (File f : listOfFiles) {

                if (f.isFile()) {
                    files = f.getName().substring(0, f.getName().length() - 4);

                    BufferedImage img = null;
                    try {
                        img = ImageIO.read(f);
                    } catch (IOException e) {
                    }

                    textures.put(files.toLowerCase(), img);
                }

            }
        }

    }

    public void drawImage(String text, Vector2 pos, float rotation, float scale) {
        AffineTransform v = g.getTransform();

        BufferedImage texture = textures.get(text.toLowerCase());

        if(texture == null){
            
            System.err.println("Failed to find texture: " + text);
            return;
            
        }
        
        AffineTransform transform = new AffineTransform();
        transform.setToTranslation((int) pos.getX(), (int) pos.getY());
        transform.scale(scale, scale);
        transform.rotate(Math.toRadians(rotation), texture.getWidth() / 2, texture.getHeight() / 2);

        g.drawImage(texture, transform, null);

        g.setTransform(v);
    }

    public void drawFilledRectangle(Rectangle2D r , Color c){
        
        Color p = g.getColor();
        g.setColor(c);
        g.fillRect((int)r.getX(), (int)r.getY(), (int)r.getWidth(), (int)r.getHeight());
        g.setColor(p);
        
    }
    
    public void drawRectangle(Rectangle2D r , Color c){
        
        Color p = g.getColor();
        g.setColor(c);
        g.draw(r);
        
        g.setColor(p);
        
    }
    
    public void drawText(String text, Vector2 pos, Color c) {

        Color p = g.getColor();
        g.setColor(c);
        g.drawString(text, pos.getX(), pos.getY());
        g.setColor(p);
    }
    
     public void drawTextCentered(String text, Vector2 pos, Color c) {

        Color p = g.getColor();
        g.setColor(c);
        
        int strWidth = getGraphics().getFontMetrics().stringWidth(text);
        
        pos = pos.sub(new Vector2(strWidth / 2, 0));
        
        g.drawString(text, pos.getX(), pos.getY());
        
        
        g.setColor(p);
    }
     
    public void drawPlayerLabel(String text, Vector2 pos, Color c , Color bg){
        
        int strWidth = getGraphics().getFontMetrics().stringWidth(text);
        int strHeight = getGraphics().getFontMetrics().getHeight() - 5;
        
        pos = pos.sub(new Vector2(strWidth / 2, 0));
        
        Vector2 position = pos.sub(new Vector2(0 , strHeight - 1));
        Vector2 box = new Vector2(strWidth , strHeight);
        
        
        Rectangle2D r = new Rectangle(position.getPoint() ,  box.getDim());
   
        this.drawFilledRectangle(r, bg);
        
        Color p = g.getColor();
        g.setColor(c);
        g.drawString(text, pos.getX(), pos.getY());
        g.setColor(p);
        
    }
    
    public void setCam(Camera c){
        
        cam = c;
        
    }
    
    public float getZoom(){
        
        return cam.getZoom();
        
    }
    
    public Vector2 getCamPos(){
        
        return cam.getPosition();
        
    }
    public void setCamPos(Vector2 c){
        
        cam.setPosition(c);
        
    }
    
    public Vector2 WorldToScreenCoordinates(Vector2 v){
        
        return v.sub(cam.getPosition()).add(new Vector2(1000 , 800).div(2)).sub(new Vector2(32 , 32)).mult(cam.getZoom());
        
    }
}
